package com.handsome.命令模式;

/**
 * @author niechun
 * @date 2023/3/15 20:10
 */
public class RemoteController {
    //开按钮的数组
    private Command[] onCommands;
    //关按钮的数组
    private Command[] offCommands;

    //执行撤销的命令
    private Command undoCommand;

    public RemoteController() {
        onCommands=new Command[5];
        offCommands=new Command[5];
        //初始化时设置为空操作按钮
        for (int i = 0; i < 5; i++) {
            onCommands[i]=new NoCommand();
            offCommands[i]=new NoCommand();
        }
    }

    public void setCommand(int number,Command onCommand,Command offCommand){
        onCommands[number]=onCommand;
        offCommands[number]=offCommand;
    }

    //按下开按钮
    public void onButtonPush(int number){
        onCommands[number].execute();
        undoCommand=onCommands[number];
    }
    //按下关按钮
    public void offButtonPush(int number){
        offCommands[number].execute();
        undoCommand=offCommands[number];
    }

    //撤销按钮
    public void cancel(){
        undoCommand.undo();
    }

}
